/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.infor.service;

import java.util.List;

import com.jeeplus.modules.infor.entity.StoreBudget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.infor.entity.Handling;
import com.jeeplus.modules.infor.mapper.HandlingMapper;
import com.jeeplus.modules.infor.entity.HandlingExpense;
import com.jeeplus.modules.infor.mapper.HandlingExpenseMapper;
import com.jeeplus.modules.infor.entity.HandlingIncome;
import com.jeeplus.modules.infor.mapper.HandlingIncomeMapper;

/**
 * 装卸收支明细Service
 * @author 李超辉
 * @version 2019-02-14
 */
@Service
@Transactional(readOnly = true)
public class HandlingService extends CrudService<HandlingMapper, Handling> {

	@Autowired
	private HandlingExpenseMapper handlingExpenseMapper;
	@Autowired
	private HandlingIncomeMapper handlingIncomeMapper;
	
	public Handling get(String id) {
		Handling handling = super.get(id);
		handling.setHandlingExpenseList(handlingExpenseMapper.findList(new HandlingExpense(handling)));
		handling.setHandlingIncomeList(handlingIncomeMapper.findList(new HandlingIncome(handling)));
		return handling;
	}
	
	public List<Handling> findList(Handling handling) {
		return super.findList(handling);
	}
	
	public Page<Handling> findPage(Page<Handling> page, Handling handling) {
		return super.findPage(page, handling);
	}
	public Page<Handling> findPage1(Page<Handling> page, Handling storeBudget) {
		List<Handling> liat=getListByYue();
		page.setList(liat);
		return page;
	}
	public Page<Handling> findPage2(Page<Handling> page, Handling storeBudget) {
		List<Handling> liat=getListBybsc();
		page.setList(liat);
		return page;
	}

	public Page<Handling> findPage3(Page<Handling> page, Handling storeBudget) {
		List<Handling> liat=getListByyear();
		page.setList(liat);
		return page;
	}
	public List<Handling> getListByYue(){
		return  mapper.getListByYue();
	};
	public List<Handling> getListBybsc(){
		return  mapper.getListBybsc();
	};
	public List<Handling> getListByyear(){
		return  mapper.getListByyear();
	};
	@Transactional(readOnly = false)
	public void save(Handling handling) {
		super.save(handling);
		for (HandlingExpense handlingExpense : handling.getHandlingExpenseList()){
			if (handlingExpense.getId() == null){
				continue;
			}
			if (HandlingExpense.DEL_FLAG_NORMAL.equals(handlingExpense.getDelFlag())){
				if (StringUtils.isBlank(handlingExpense.getId())){
					handlingExpense.setHandling(handling);
					handlingExpense.preInsert();
					handlingExpenseMapper.insert(handlingExpense);
				}else{
					handlingExpense.preUpdate();
					handlingExpenseMapper.update(handlingExpense);
				}
			}else{
				handlingExpenseMapper.delete(handlingExpense);
			}
		}
		for (HandlingIncome handlingIncome : handling.getHandlingIncomeList()){
			if (handlingIncome.getId() == null){
				continue;
			}
			if (HandlingIncome.DEL_FLAG_NORMAL.equals(handlingIncome.getDelFlag())){
				if (StringUtils.isBlank(handlingIncome.getId())){
					handlingIncome.setHandling(handling);
					handlingIncome.preInsert();
					handlingIncomeMapper.insert(handlingIncome);
				}else{
					handlingIncome.preUpdate();
					handlingIncomeMapper.update(handlingIncome);
				}
			}else{
				handlingIncomeMapper.delete(handlingIncome);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(Handling handling) {
		super.delete(handling);
		handlingExpenseMapper.delete(new HandlingExpense(handling));
		handlingIncomeMapper.delete(new HandlingIncome(handling));
	}
	
}